package com.yupi.aicodehelper.ai.dto;

import dev.langchain4j.data.message.*;

import java.util.Arrays;
import java.util.List;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.data.message.AiMessage;


import java.util.List;

public class StoredMessage {

    private int memoryId;  // 新增 memoryId
    private String type;
    private String text;
    private long createdAt; // 消息生成时间

    public StoredMessage() {}

    public StoredMessage(int memoryId, String type, String text, long createdAt) {
        this.memoryId = memoryId;
        this.type = type;
        this.text = text;
        this.createdAt = createdAt;
    }

    public int getMemoryId() {
        return memoryId;
    }

    public String getType() {
        return type;
    }

    public String getText() {
        return text;
    }

    public long getCreatedAt() {
        return createdAt;
    }

    public void setMemoryId(int memoryId) {
        this.memoryId = memoryId;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setCreatedAt(long createdAt) {
        this.createdAt = createdAt;
    }

    public static StoredMessage fromChatMessage(int memoryId, ChatMessage msg) {
        long now = System.currentTimeMillis();
        // 改造Java 14+模式匹配为Java 8兼容语法
        if (msg instanceof SystemMessage) {
            SystemMessage m = (SystemMessage) msg;
            return new StoredMessage(memoryId, "SYSTEM", m.text(), now);
        } else if (msg instanceof UserMessage) {
            UserMessage m = (UserMessage) msg;
            StringBuilder sb = new StringBuilder();
            // 将var替换为显式Content类型声明
            for (Content c : m.contents()) {
                if (c instanceof TextContent) {
                    TextContent t = (TextContent) c;
                    sb.append(t.text());
                }
            }
            return new StoredMessage(memoryId, "USER", sb.toString(), now);
        } else if (msg instanceof AiMessage) {
            AiMessage m = (AiMessage) msg;
            return new StoredMessage(memoryId, "AI", m.text(), now);
        }
        throw new IllegalArgumentException("Unknown ChatMessage type: " + msg.getClass());
    }


    public ChatMessage toChatMessage() {
        // 将Java 14+ switch表达式改为Java 8传统switch语句，并替换List.of为Arrays.asList
        switch (type) {
            case "SYSTEM":
                return new SystemMessage(text);
            case "USER":
                // Java 8无List.of()，使用Arrays.asList()替代
                return new UserMessage(Arrays.asList(new TextContent(text)));
            case "AI":
                return new AiMessage(text);
            default:
                throw new IllegalArgumentException("Unknown type: " + type);
        }
    }

    @Override
    public String toString() {
        return "StoredMessage{" +
                "memoryId=" + memoryId +
                ", type='" + type + '\'' +
                ", text='" + text + '\'' +
                ", createdAt=" + createdAt +
                '}';
    }
}